package service.impl;

import dao.BookDao;
import dao.LendDao;
import dao.UserDao;
import dao.impl.BookDaoImpl;
import dao.impl.LendDaoImpl;
import dao.impl.UserDaoImpl;
import entity.Book;
import entity.Record;
import service.LendService;
import util.DataSourceUtil;

import java.sql.SQLException;
import java.util.List;

public class LendServiceImpl implements LendService {
    LendDao lendDao=new LendDaoImpl();
    BookDao bookDao=new BookDaoImpl();
    UserDao userDao=new UserDaoImpl();

    public List<Record> queryAllLend(){
        try{
            return lendDao.queryAllLend();
        } catch (SQLException exception) {
            exception.printStackTrace();
            return null;
        }catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public boolean borowBook(String userId, String bookId) {
        try {
            if(bookDao.isExist(bookId)&&userDao.isExist(userId)){
                if (bookDao.isIn(bookId)) {
                    if(lendDao.addRecord(userId,bookId)){
                        if(bookDao.updateStat(bookId,"false")){
                            return true;
                        }else{
                            return false;
                        }
                    }else{
                        return false;
                    }
                }else{
                    return false;
                }

            }else{
                return false;
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public boolean returnBook(String userId, String bookId) {
        try {
            if (bookDao.isExist(bookId) && userDao.isExist(userId)) {
                if(lendDao.userIsNotReturn(userId,bookId)){
                    if(lendDao.updateRecord(userId,bookId)){
                        if(bookDao.updateStat(bookId,"true")){
                            return true;
                        }else{
                            return false;
                        }
                    }else{
                        return false;
                    }
                }else{
                    return false;
                }
            }else{
                return false;
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public List<Record> userLendInformation(String userId) {
        try {
            if(userDao.isExist(userId)){
                List<Record> records=lendDao.borrowInformation(userId);
                return records;
            }else{
                return null;
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public boolean deleteRecord(String userId, String bookId,String borrowDate){
        try {
            if(lendDao.deleteRecord(userId,bookId,borrowDate)){
                return true;
            }else{
                return false;
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public List<Record> userLendCount(String userId) {
        try {
            if(userDao.isExist(userId)){
                List<Record> records=lendDao.borrowCount(userId);
                return records;
            }else{
                return null;
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public int getTotalCount(String sql,Object[] params){
        try {
            int count=bookDao.getTotalCount(sql,params);
            return count;
        } catch (SQLException e) {
            e.printStackTrace();
            return -1;
        }catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
    }

    //查询当前页的数据集合 currentPage(当前页码) pageSize(页面大小)
    public List<Record> queryRecordByPage(int currentPage,int pageSize) {
        try {
            List<Record> records=lendDao.queryRecordByPage(currentPage,pageSize);
            return records;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    //根据用户或书籍账号查询当前页的数据集合 currentPage(当前页码) pageSize(页面大小)
    public List<Record> queryRecordByPageRequire(int currentPage,int pageSize,Object param) {
        try {
            List<Record> records=lendDao.queryRecordByPageRequire(currentPage,pageSize,param);
            return records;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public List<Record> queryCountByPageRequire(int currentPage,int pageSize,Object param){
        try {
            List<Record> records=lendDao.queryCountByPageRequire(currentPage,pageSize,param);
            return records;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public List<Record> queryCountByPage(int currentPage,int pageSize){
        try {
            List<Record> records=lendDao.queryCountByPage(currentPage,pageSize);
            return records;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public List<Record> queryRecordByUserPage(int currentPage,int pageSize,String userId) {
        try {
            List<Record> records=lendDao.queryRecordByUserPage(currentPage,pageSize,userId);
            return records;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public List<Record> queryRecordByUserPageRequire(int currentPage,int pageSize,String userId,Object param) {
        try {
            List<Record> records=lendDao.queryRecordByUserPageRequire(currentPage,pageSize,userId,param);
            return records;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

//    public static void main(String[] args) throws InterruptedException {
//        System.out.println(new LendServiceImpl().borowBook("20201111911","20220015"));
//        System.out.println(new LendServiceImpl().returnBook("20201111911","20220015"));
//        List<Record> records=new LendServiceImpl().userLendInformation("admin_1");
//        for(Record record:records)
//            System.out.println(record.getBookId());
//        List<Record> records=new LendServiceImpl().userLendCount("admin_1");
//        for(Record record:records){
//            System.out.println(record.getUserId()+"----"+record.getBookId()+"----------"+record.getBorrowCount());
//        }
//    }
}
